#ifndef _NET_TCPSOCKET_H_
#define _NET_TCPSOCKET_H_

#include "common/common.h"

#include "isocket.h"

namespace net {

const uint32_t MAX_TCPSOCKET_BUFFER = 1024 * 32;

enum State {
    Invalild,
    Connected,
    Closing,
    Erroring,

    SS_NORMAL,
    SS_CLOSED,	
    SS_ERROR,
};

}

namespace net {

class IBuffer;
class IReactor;
class ISocketHandler;

class TCPSocket : public ISocket , public IEvent {
public:
    TCPSocket();
    TCPSocket(uint32_t fd);
    ~TCPSocket();

private:
    void Init();
    bool SetBlock(bool is_block);

    void OnRead();
    void OnWrite();
    void OnError();
    void OnClose();
    void OnConnect();

    void Send();
    void Destroy();

    virtual SocketInfo& GetSocketInfo();

    virtual void SetHandler(ISocketHandler* handler);

    virtual IBuffer* GetInBuffer();
    virtual IBuffer* GetOutBuffer();

    virtual uint32_t Connect(NetAddress& address);
    virtual uint32_t Send(const char* data, uint32_t len);

    virtual void Close();

protected:
    virtual uint32_t GetFd();
    virtual void OnEvent(uint32_t event_type);

protected:
    uint32_t fd;
    uint32_t error;
    State state;

    IReactor* reactor;
    IBuffer* in_buffer;
    IBuffer* out_buffer;
    ISocketHandler* handler;
};

}
#endif
